package cn.juforg.gravitysnake;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

/**
 * Snake: a simple game that everyone can enjoy.
 * GravitySnake: the upgrade of Snake
 * @author Juforg
 */
public class SnakeMainAty extends Activity {
	
	private static String ICICLE_KEY = "snake-garden";
	
	private SnakeView snakeView ;
	private SensorManager sensorManager = null;   //真机感应器管理器
//	private SensorManagerSimulator sensorManager = null;   //模拟器感应器管理器
	private Sensor gravitySensor = null;		//重力感应器
	char gameMode ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.i("Gravity Snake", "SnakeMainAty onCreate");
    	/** Called when the activity is first created. */
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = getIntent();
        gameMode = intent.getCharExtra("gameMode", '1');
        snakeView = (SnakeView) findViewById(R.id.snake);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        System.out.println("metric.xdpi"+metric.xdpi+"|widthPixels:"+metric.widthPixels+"|density:"+metric.density+"|densityDpi:"+metric.densityDpi);
        switch (metric.widthPixels) {
        case 240:
        	Toast.makeText(SnakeMainAty.this, "pix:240", Toast.LENGTH_SHORT).show();
        	break;
        case 480:
        	Toast.makeText(SnakeMainAty.this, "pix:480", Toast.LENGTH_SHORT).show();
        	break;
        }
        //判断游戏模式
        if (gameMode == SnakeStartAty.RESTARTGAME) {
        	snakeView.onSizeChanged(metric.widthPixels, metric.heightPixels, 0, 0);
//        	snakeView.initNewGame();
//        	snakeView.setMode(SnakeView.RUNNING);
//        	snakeView.updateGame();
		}
        //检测存储状态是否为空
        if (savedInstanceState != null) {
        	snakeView.setMode(SnakeView.READY);
		}else {
			//恢复游戏状态
			
		}
		//取得重力感应器对象
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);//真机
//		sensorManager = SensorManagerSimulator.getSystemService(this,Context.SENSOR_SERVICE);//模拟器
//		sensorManager.connectSimulator(); //模拟器
		gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//真机
//		sensorManager.registerListener(snakeView, gravitySensor, SensorManager.SENSOR_DELAY_UI);
        
    }
	@Override
	protected void onPause() {
		super.onPause();
		snakeView.setMode(SnakeView.PAUSE);
		//取消监听
		Log.i("Gravity Snake", "Gravity Snake Service onPause");
		sensorManager.unregisterListener(snakeView);
	}
	
	@Override
	protected void onDestroy() {
		//取消监听
		Log.i("Gravity Snake", "Gravity Snake Service onDestroy");
		sensorManager.unregisterListener(snakeView);
		super.onDestroy();
	}
	
	@Override
	protected void onResume() {
		Log.i("Gravity Snake", "Gravity Snake Service onResume");
		 //判断游戏模式
        if (gameMode == SnakeStartAty.RESTARTGAME) {
        	snakeView.initNewGame();
        	snakeView.setMode(SnakeView.RUNNING);
		}
		sensorManager.registerListener(snakeView, gravitySensor, SensorManager.SENSOR_DELAY_UI);
		super.onResume();
	}
	
	@Override
	protected void onRestart() {
		Log.i("Gravity Snake", "Gravity Snake Service onRestart");
		sensorManager.registerListener(snakeView, gravitySensor, SensorManager.SENSOR_DELAY_UI);
		super.onRestart();
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBundle(ICICLE_KEY, snakeView.saveStatus());
	}
    
    
}